API Reading

API Reading

Keypoint

  • Authentication(How to use the header?)
  • Content type(JSON or Multipart)
  • Required or Optional parameter

status code

429(too many request)
500

Q1 RemoveBackground

題目描述: 請閱讀以下 API 文件摘要,寫一段 Pseudocode 函式 RemoveBackground(image_data),回傳去背後的圖片網址。若失敗請回傳 Error。

API Endpoint: POST https://api.vivipic.com/v1/remove-bg

Headers:

  • Authorization: Bearer
  • Content-Type: multipart/form-data Body

Parameters:

  • file: The image file (Binary). (Required)
  • output_format: “png” or “webp”. Default is “png”. (Optional)

Response (JSON):

  • Success (200): { “status”: “success”, “url”: “https://…” }
  • Error (4xx/5xx): { “status”: “error”, “message”: “Invalid file” }

A1

code
FUNCTION RemoveBackGround(image_binary) // 1. Setup configuration api_url = https://api.vivipic.com/v1/remove-bg api_key = GET_ENV("VIVIPIC_API_KEY") // 2. prepare header header = { "Authorization": "Bear" + api_key, "Content-Type": "multipart/form-data Body" } // 3. prepare payload payload = { "file": image_binary, "output_format": "png" //explicitly setting optional parameter } // 4. request response = HTTP(api_url, header, payload) // 5. handling status code status_code = response.status_code IF status_code == 200 THEN json_body = PARSE_JSON(response.body) RETURN json_body.url ELSE IF status_code == 402 THEN RETURN ERROR("Unauthorized: check API Key") ELSE IF status_code == 500 THEN RETURN ERROR("Server Error: please try again later") ELSE //capture generic error json_body = PARSE_JSON(response.body) RETURN ERROR("API Failed" + json_body.message) END IF END FUNCTION

Q2 AI 服務串接與錯誤處理 (AI Pipeline Orchestration)

對應 JD: 定義APP和Server的溝通協定、導入實際手機APP程式、去背景

核心考點: API 串接邏輯、Promise/Async Chain、錯誤復原 (Fallback)。

情境: App 要執行一個「人像風格化」的功能,流程如下:

  • 呼叫 RemoveBG_API 去背。
  • (成功後) 將去背圖傳給 StyleTransfer_API 進行風格化。
  • (成功後) 將結果回傳。 挑戰: 如果第 1 步去背失敗,不要報錯,而是直接拿原圖去跑第 2 步(Fallback 機制)。請設計這個流程。

A2

/*
Function: ProcessPortraitStyle
Description: Orchestrates a multi-stage AI pipeline with fallback logic
*/

FUNCTION ProcessPortrainStyle(ori_image) -> Image
    current_image = ori_image

    // 1. try remove background
    TRY
        // Assume CALL_API is async function that throws error on 4xx/5xx
        api_url = "https://api.vivipic.com/remove-bg"
        bg_remove_img = AWAIT CALL_API(api_url, ori_image)

        // If successful
        current_image = bg_remove_img
    CATCH Error e
        // Fallback logic
        LOG("Remove BG error" + e.message)
        current_image = ori_image
    END TRY

    // 2. style transfer
    TRY
        api_url_transfer = "https://api.vivipic.com/style-transfer"
        final_result = AWAIT CALL_API(api_url_transfer, current_image)

        RETURN currrent_image
    CATCH Error e
        THROW Error("Transfer failed: " + e.message)
    END TRY
END FUNCTION

/*
RESPONSE JSON SCHEMA DESIGN:
{
    "task_id": "12345",
    "status": "completed", // or "partial success", "failed"
    "data": {
        "image_url": "https://...",
        "processing_details": {
            "remove_bg": "skipped_error", //Client knows BG removal failed
            "style_transfer": "success"
        }
    }
}
*/

「在設計 AI Pipeline 時,容錯性 (Fault Tolerance) 非常重要。針對去背功能,我設計了 Fallback 機制:如果去背 API 掛了或超時,系統會自動『降級服務 (Graceful Degradation)』使用原圖繼續後續流程,而不是直接讓 User 看到錯誤畫面。同時,我在 API 回傳的 JSON 結構中定義了 processing_details,讓 App 端知道發生了什麼事。」


API Reading
https://z-hwa.github.io/webHome/[object Object]/Interview/API-Reading/
作者
crown tako
發布於
2026年1月17日
許可協議